package com.tsinjie.metadata.database.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuXunming on 2014-07-20.
 * 数据库表
 */
public class Table implements Serializable {
    /**
     * 表名
     */
    private String tableName;
    /**
     * 表注释
     */
    private String comment;
    /**
     * 字段
     */
    private List<Column> columns;
    /**
     * 主键
     */
    private List<Column> primaryKeys;
    /**
     * 外键
     */
    private List<ImportColumn> importColumns;
    /**
     * 被其他表引用的键
     */
    private List<ExportColumn> exportColumns;

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public List<Column> getPrimaryKeys() {
        return primaryKeys;
    }

    public void setPrimaryKeys(List<Column> primaryKeys) {
        this.primaryKeys = primaryKeys;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }

    /**
     * 添加字段
     *
     * @param column
     */
    public void addColumn(Column column) {
        if (null == columns) {
            columns = new ArrayList<Column>();
        }
        columns.add(column);
    }

    public List<ImportColumn> getImportColumns() {
        return importColumns;
    }

    /**
     * 获取外键
     *
     * @param columnName
     * @return
     */
    public Column getImportColumn(String columnName) {
        if (null == importColumns) {
            return null;
        }
        for (Column column : importColumns) {
            if (column.getColumnName().equals(columnName)) {
                return column;
            }
        }
        return null;
    }

    public void setImportColumns(List<ImportColumn> importColumns) {
        this.importColumns = importColumns;
    }

    public List<ExportColumn> getExportColumns() {
        return exportColumns;
    }

    public void setExportColumns(List<ExportColumn> exportColumns) {
        this.exportColumns = exportColumns;
    }

    /**
     * 添加主键
     *
     * @param column
     */
    public void addPrimaryKey(Column column) {
        if (null == primaryKeys) {
            primaryKeys = new ArrayList<Column>();
        }
        primaryKeys.add(column);
    }

    /**
     * 获取主键
     *
     * @return
     */
    public Column getPrimaryKey() {
        if (null == primaryKeys) {
            primaryKeys = new ArrayList<Column>();
        }
        return primaryKeys.get(0);
    }

    /**
     * 获取主键
     *
     * @param columnName
     * @return
     */
    public Column getPrimaryKey(String columnName) {
        if (null == primaryKeys) {
            primaryKeys = new ArrayList<Column>();
        }
        for (Column column : primaryKeys) {
            if (column.getColumnName().equals(columnName)) {
                return column;
            }
        }
        return null;
    }

    /**
     * 根据字段名获取字段信息
     *
     * @param columnName
     * @return
     */
    public Column getColumn(String columnName) {
        if (null == columns) {
            return null;
        }
        for (Column column : columns) {
            if (column.getColumnName().equals(columnName)) {
                return column;
            }
        }
        return null;
    }

    /**
     * 添加外键
     *
     * @param column
     */
    public void addImportColumn(ImportColumn column) {
        if (null == importColumns) {
            importColumns = new ArrayList<ImportColumn>();
        }
        importColumns.add(column);
    }

    /**
     * 添加被引用的键
     *
     * @param column
     */
    public void addExportColumn(ExportColumn column) {
        if (null == exportColumns) {
            exportColumns = new ArrayList<ExportColumn>();
        }
        exportColumns.add(column);
    }

    @Override
    public String toString() {
        return "Table{" +
                "tableName='" + tableName + '\'' +
                ", comment='" + comment + '\'' +
                ", columns=" + columns +
                ", primaryKeys=" + primaryKeys +
                ", importColumns=" + importColumns +
                ", exportColumns=" + exportColumns +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Table)) return false;

        Table table = (Table) o;

        if (tableName != null ? !tableName.equals(table.tableName) : table.tableName != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return tableName != null ? tableName.hashCode() : 0;
    }
}
